﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Entity;
using CSP.BL;

namespace CSPTest.Admin
{
    public partial class AspxPage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        //TODO: remove logic code to BL
        protected void UpdateToDB_Click(object sender, EventArgs e)
        {
            var aspxPage = Request.Form["AspxPage"];
            if (aspxPage == null)
                return;

            using (var dc = new CSPEntities())
            {
                foreach (var page in aspxPage.Split(','))
                {
                    var pp = page.Split('$');
                    string fullName = pp[0];
                    string belongsTo = pp[1];
                    var count = dc.AspxPages.Count(p => p.FullName == fullName);
                    if (count == 0)
                    {
                        dc.AddToAspxPages(new Entity.AspxPage { FullName = fullName, AccessLevel = (int)AccessLevel.Read, BelongsTo = belongsTo, });
                        dc.AddToAspxPages(new Entity.AspxPage { FullName = fullName, AccessLevel = (int)AccessLevel.Write, BelongsTo = belongsTo, });
                    }
                }

                dc.SaveChanges();
            }

        }
    }
}
